package org.example.dependencyinversion;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @author
 * @description
 */
@SpringBootTest
public class DependencyPass {

    @Autowired
    private OpenAndCloseInterface openAndCloseInterface;


    @Test
    void openChangHongInterface() {
        ChangHongInterface changHongInterface = new ChangHongInterface();
        openAndCloseInterface.open();
    }

    @Test
    void openChangHongConstructor(){
        ChangHongConstructor changHongConstructor = new ChangHongConstructor();
        OpenAndCloseConstructor openAndCloseConstructor = new OpenAndCloseConstructor(changHongConstructor);
        openAndCloseConstructor.open();
    }

    @Test
    void openChangHongSett(){
        ChangHongSett changHongSett=new ChangHongSett();
        OpenAndCloseSett openAndCloseSett = new OpenAndCloseSett();
        openAndCloseSett.setTv(changHongSett);
        openAndCloseSett.open();
    }
}

interface IOpenAndClose{
    void open();
}

interface ITV{
    void play();

}

//方式1、通过接口传递实现依赖
@Component
class OpenAndCloseInterface implements IOpenAndClose{
    public void open(){
    }

}

//方式2、通过构造器依赖传递
class OpenAndCloseConstructor implements IOpenAndClose{
    private ITV  itv;

    public OpenAndCloseConstructor(ITV  itv){//构造器方式
        this.itv = itv;
    }


    public void open(){
        this.itv.play();
    }

}


//方式3 通过setter方法传递依赖
@Service
class OpenAndCloseSett implements IOpenAndClose{
    private ITV itv;

    public void setTv(ITV itv) { //sett方法
        this.itv = itv;
    }

    public void open() {
        this.itv.play();
    }

}



//方式1、通过接口传递实现依赖
class ChangHongSett implements  ITV{
    public void play(){
        System.out.println("sett长虹电视机，打开");
    }
}

//方式2、通过构造器依赖传递
class ChangHongConstructor implements  ITV{
    public void play(){
        System.out.println("构造器长虹电视机，打开");
    }
}

//方式3 通过setter方法传递依赖
class ChangHongInterface implements  ITV{
    public void play(){
        System.out.println("接口长虹电视机，打开");
    }
}


abstract class A  extends  Object implements IOpenAndClose{

}